package com.sunday.shoufen.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.shoufen.R;
import com.sunday.shoufen.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.shoufen.base.Constants.CODE_1;
import static com.sunday.shoufen.base.Constants.CODE_3;
import static com.sunday.shoufen.base.Constants.CODE_4;
import static com.sunday.shoufen.base.Constants.SEND_CODE_SIGN;

/**
 * Created by 刘涛 on 2017/10/30.
 */

public class RegisterActivity extends BaseActivity {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_mobile)
    ClearEditText editMobile;
    @Bind(R.id.edit_code)
    ClearEditText editCode;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.btn_next)
    TextView btnNext;
    private TimeCount timeCount;

    private int type;//0:注册 1：修改密码 2：忘记密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        type=getIntent().getIntExtra("type",0);
        initView();
    }

    private void initView() {
        switch (type){
            case 0:
                titleView.setText("注册");
                break;
            case 1:
                titleView.setText("修改密码");
                break;
            case 2:
                titleView.setText("忘记密码");
                break;
        }

        timeCount=new TimeCount(60000,1000,sendCode);

    }

    private String mobile,code;
    @OnClick({R.id.send_code,R.id.btn_next})
    void onClick(View v){
        switch (v.getId()){
            case R.id.send_code:
                mobile=editMobile.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)|| !StringUtils.isMobileNO(mobile)){
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                    return;
                }
                switch (type){
                    case 0:
                        sendCode(mobile);
                        break;
                    case 1:
                    case 2:
                        //修改密码
                        sendCode1(mobile);
                        break;
                }
                break;
            case R.id.btn_next:
                 mobile=editMobile.getText().toString().trim();
                code=editCode.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)|| !StringUtils.isMobileNO(mobile)){
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)){
                    ToastUtils.showToast(mContext,"请输入正确的验证码");
                    return;
                }

                intent=new Intent(mContext,RegisterFinishActivity.class);
                intent.putExtra("mobile",mobile);
                intent.putExtra("code",code);
                intent.putExtra("type",type);
                startActivity(intent);
                finish();
                break;
        }
    }

    private void sendCode(String mobile){
        Call<ResultDO> call= ApiClient.getApiAdapter().sendCode(mobile,SEND_CODE_SIGN,CODE_3,1);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"验证码已发送到手机");
                    timeCount.start();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }

    private void sendCode1(String mobile){
        Call<ResultDO> call= ApiClient.getApiAdapter().sendCode(mobile,SEND_CODE_SIGN,CODE_4,2);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"验证码已发送到手机");
                    timeCount.start();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }

    private void sendCode2(String mobile){
        Call<ResultDO> call= ApiClient.getApiAdapter().sendCode(mobile,SEND_CODE_SIGN,CODE_1,3);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"验证码已发送到手机");
                    timeCount.start();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount!=null){
            timeCount.cancel();
        }
    }

}
